package com.white.note;

import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.white.note.base.BaseActivity;
import com.white.note.fragment.HomeFragment;
import com.white.note.fragment.MineFragment;
import com.white.note.fragment.SearchFragment;


public class MainActivity extends BaseActivity {
    private HomeFragment mHomeFragment;
    private SearchFragment mSearchFragment;
    private MineFragment mMineFragment;

    private BottomNavigationView mBottomNavigationView;

    @Override
    protected int getLayoutResid() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews() {
        // 底部导航栏
        mBottomNavigationView = findViewById(R.id.bottomNavigationView);
    }

    @Override
    protected void setListener() {
        // 底部导航栏点击 切换展示视图
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.home) {
                    selectedFragment(0);
                } else if (item.getItemId() == R.id.search) {
                    selectedFragment(1);
                } else {
                    selectedFragment(2);
                }

                return true;
            }
        });
    }

    @Override
    protected void initData() {
        // 默认选中第一个
        selectedFragment(0);
    }

    private void selectedFragment(int position) {
        // 选中指定底部导航栏
        // 创建并返回一个 FragmentTransaction 实例 开启一个 Fragment 事务
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        // 隐藏所有视图
        hideFragment(fragmentTransaction);

        if (position == 0) {
            // 首页展示
            if (mHomeFragment == null) {
                mHomeFragment = new HomeFragment();
                fragmentTransaction.add(R.id.content, mHomeFragment);
            } else {
                fragmentTransaction.show(mHomeFragment);
            }
        } else if (position == 1) {
            // 搜索页展示
            if (mSearchFragment == null) {
                mSearchFragment = new SearchFragment();
                fragmentTransaction.add(R.id.content, mSearchFragment);
            } else {
                fragmentTransaction.show(mSearchFragment);
            }
        } else {
            // 个人中心展示
            if (mMineFragment == null) {
                mMineFragment = new MineFragment();
                fragmentTransaction.add(R.id.content, mMineFragment);
            } else {
                fragmentTransaction.show(mMineFragment);
            }
        }
        // 一定要提交
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        // 隐藏所有视图
        if (mHomeFragment != null) {
            fragmentTransaction.hide(mHomeFragment);
        }
        if (mSearchFragment != null) {
            fragmentTransaction.hide(mSearchFragment);
        }
        if (mMineFragment != null) {
            fragmentTransaction.hide(mMineFragment);
        }

    }
}